#!/bin/bash
# 设置PRIVATE-TOKEN
PRIVATE_TOKEN="7P9qgkeaaRKygNgDXaiV"
BASE_PATH="$HOME/code/all"

# 获取组下所有项目的函数
function get_projects_for_group() {
    local group_id="$1"
    local full_path="$2"

    # 发送请求并获取响应
    local projects_response=$(curl --location --request GET "http://gitlab.inzwc.com/api/v4/groups/${group_id}/projects" \
        --header "User-Agent: Apifox/1.0.0 (https://apifox.com)" \
        --header "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" \
        --header 'Accept: */*' \
        --header 'Host: gitlab.inzwc.com' \
        --header 'Connection: keep-alive' \
        --write-out "HTTPSTATUS:%{http_code}")

    # 提取项目请求的HTTP状态码
    local projects_http_status=$(echo "$projects_response" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')

    # 检查项目请求的HTTP状态码是否为200
    if [ "$projects_http_status" -ne 200 ]; then
        echo "获取项目列表失败，HTTP状态码：$projects_http_status"
        return 1
    fi

    # 提取项目响应体
    local projects_response_body=$(echo "$projects_response" | sed -e 's/HTTPSTATUS\:.*//g')

    # 解析项目JSON并提取所需字段
    if [ "$(echo "$projects_response_body" | jq '. | length')" -eq 0 ]; then
        echo "group_id：$project_id 下没有项目"
        return 1
    fi

    # 使用数组存储克隆任务的 PID
    declare -a clone_pids=()

    echo "$projects_response_body" | jq -c '.[]' | while read -r project; do
        local project_id=$(echo "$project" | jq -r '.id')
        local project_name=$(echo "$project" | jq -r '.name')
        local project_http_url_to_repo=$(echo "$project" | jq -r '.http_url_to_repo')
        local trimmed_http_url_to_repo=$(echo "$project_http_url_to_repo" | sed 's|^http://||')

        # 打印提取的变量
        echo "Project ID: $project_id"
        echo "Project Name: $project_name"
        echo "Project HTTP URL: $project_http_url_to_repo"

        # 构建克隆目录
        clone_directory="$BASE_PATH/$full_path/$project_name"
        if [ ! -d "$clone_directory" ]; then
            mkdir -p "$clone_directory"
        else
            echo "目录 '$clone_directory' 已经存在。"
            continue
        fi

        # 后台克隆项目
        git clone "http://oauth2:$PRIVATE_TOKEN@$trimmed_http_url_to_repo" "${clone_directory}" &
        clone_pids+=($!)  # 将克隆任务的进程ID存入数组
    done

    # 等待所有后台任务完成
    for pid in "${clone_pids[@]}"; do
        wait "$pid"
        if [ $? -ne 0 ]; then
            echo "Failed to clone project with PID $pid."
        fi
    done

    echo "-----------------------------"
}

# 递归获取组下所有子分组的函数
function recursively_get_subgroups() {
    local group_id="$1"
    local full_path="$2"

    # 发送请求并获取响应
    local subgroups_response=$(curl --location --request GET "http://gitlab.inzwc.com/api/v4/groups/${group_id}/subgroups" \
        --header "User-Agent: Apifox/1.0.0 (https://apifox.com)" \
        --header "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" \
        --header 'Accept: */*' \
        --header 'Host: gitlab.inzwc.com' \
        --header 'Connection: keep-alive' \
        --write-out "HTTPSTATUS:%{http_code}")

    # 提取子分组请求的HTTP状态码
    local subgroups_http_status=$(echo "$subgroups_response" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')

    # 检查子分组请求的HTTP状态码是否为200
    if [ "$subgroups_http_status" -ne 200 ]; then
        echo "获取子分组列表失败，HTTP状态码：$subgroups_http_status"
        return 1
    fi

    # 提取子分组响应体
    local subgroups_response_body=$(echo "$subgroups_response" | sed -e 's/HTTPSTATUS\:.*//g')

    # 检查是否返回了空数组，表示没有子分组了
    if [ "$(echo "$subgroups_response_body" | jq '. | length')" -eq 0 ]; then
        echo "组 ${group_id} 下没有更多子分组。"
        return
    fi

    # 解析子分组JSON并递归查询
    echo "$subgroups_response_body" | jq -c '.[]' | while read -r subgroup; do
        local subgroup_id=$(echo "$subgroup" | jq -r '.id')
        local subgroup_name=$(echo "$subgroup" | jq -r '.name')
        local subgroup_path=$(echo "$subgroup" | jq -r '.path')

        # 打印提取的子分组信息
        echo "Subgroup ID: $subgroup_id"
        echo "Subgroup Name: $subgroup_name"
        echo "Subgroup Path: $subgroup_path"

        # 查询并下载项目
        get_projects_for_group "$subgroup_id" "$full_path/$subgroup_path"

        # 继续递归查询子分组的子分组
        recursively_get_subgroups "$subgroup_id" "$full_path/$subgroup_path"
    done
}

# 发送请求并获取响应
response=$(curl --location --request GET 'http://gitlab.inzwc.com/api/v4/groups?all_available=true&top_level_only=true' \
--header "User-Agent: Apifox/1.0.0 (https://apifox.com)" \
--header "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" \
--header 'Accept: */*' \
--header 'Host: gitlab.inzwc.com' \
--header 'Connection: keep-alive' \
--write-out "HTTPSTATUS:%{http_code}")

# 提取HTTP状态码
http_status=$(echo "$response" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')

# 检查HTTP状态码是否为200
if [ "$http_status" -ne 200 ]; then
    echo "请求失败，HTTP状态码：$http_status"
    exit 1
fi

# 提取实际的响应体
response_body=$(echo "$response" | sed -e 's/HTTPSTATUS\:.*//g')

# 解析JSON并提取所需字段
echo "$response_body" | jq -c '.[]' | while read -r group; do
    id=$(echo "$group" | jq -r '.id')
    full_name=$(echo "$group" | jq -r '.full_name')
    full_path=$(echo "$group" | jq -r '.full_path')
    parent_id=$(echo "$group" | jq -r '.parent_id')

    # 打印提取的变量
    echo "ID: $id"
    echo "Full Name: $full_name"
    echo "Full Path: $full_path"
    echo "Parent ID: $parent_id"

    # 调用函数获取组下所有项目
    get_projects_for_group "$id" "$full_path"

    # 调用递归函数获取当前组下的所有子分组
    recursively_get_subgroups "$id" "$full_path"
done
